<script>
export default {
  data() {
    const pointNoValidate = (rule, value, callback) => {
      this.$axios.get('/patrolPoint/pointno?pointNo=' + value)
        .then(res => {
          const vo = res.data
          if (vo.data == null) {
            callback()
          } else {
            callback(new Error('编号已被使用'))
          }
        })
    }
    return {
      pageNum: 1,
      pageSize: 5,
      total: 0,
      patrolPointModel: {},
      patrolPointList: [],
      dialogFormVisible: false,
      form: {},
      title: '',
      rules: {
        pointno: [
          { validator: pointNoValidate, trigger: 'blur' },
          { required: true, message: '请输入巡检点编号', trigger: "blur" }
        ],
        pointname: [
          { required: true, message: '请输入巡检点名称', trigger: "blur" }
        ],
        address: [
          { required: true, message: '请输入巡检点地址', trigger: "blur" }
        ]
      }
    }
  },
  methods: {
    list() {
      this.$axios.get('/patrolPoint', { params: { pageNum: this.pageNum, pageSize: this.pageSize, ...this.patrolPointModel } })
        .then(res => {
          this.patrolPointList = res.data.data.list
          this.total = res.data.data.total
        })
    },
    handleSizeChange(num) {
      this.pageSize = num
      this.list()
    },
    handleCurrentChange(num) {
      this.pageNum = num
      this.list()
    },
    add() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.$axios.post('/patrolPoint', this.form)
            .then(res => {
              this.list()
              this.dialogFormVisible = false
              this.$message.success('添加成功')
            })
        }
      })
    },
    showUpdate(row) {
      this.title = '修改'
      this.form = { ...row }
      this.dialogFormVisible = true
    },
    update() {
      this.$axios.put('/patrolPoint', this.form)
        .then(res => {
          this.list()
          this.dialogFormVisible = false
          this.$message.success('修改成功')
        })
    },
    del(row) {
      this.$confirm('确认要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete('/patrolPoint/' + row.id)
          .then(res => {
            this.list()
            this.$message.success('删除成功')
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
  },
  mounted() {
    this.list()
  }
}
</script>

<template>
  <div>
    <el-form :model="patrolPointModel" :inline="true">
      <el-form-item label="巡检点编号">
        <el-input v-model="patrolPointModel.pointno"></el-input>
      </el-form-item>

      <el-form-item label="巡检点名称">
        <el-input v-model="patrolPointModel.pointname"></el-input>
      </el-form-item>

      <el-form-item label="巡检点地址">
        <el-input v-model="patrolPointModel.address"></el-input>
      </el-form-item>
      <el-button type="primary" @click="list()">查询</el-button>
      <el-button type="info" @click="patrolPointModel = {}">清空</el-button>
    </el-form>
    <el-button type="primary" @click="form = {}, title = '添加', dialogFormVisible = true">添加</el-button>
    <el-table :data="patrolPointList" style="width: 100%">
      <el-table-column prop="id" label="序号" width="180">
      </el-table-column>
      <el-table-column prop="pointno" label="巡检点编号" width="180">
      </el-table-column>
      <el-table-column prop="pointname" label="巡检点名称" width="180">
      </el-table-column>
      <el-table-column prop="address" label="巡检点地址">
      </el-table-column>
      <el-table-column prop="remark" label="备注">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{row}">
          <el-button type="warning" size="mini" @click="showUpdate(row)">修改</el-button>
          <el-button type="danger" size="mini" @click="del(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum" :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="form" :rules="rules" ref="form" label-width="auto">
        <el-form-item label="巡检点编号" prop="pointno">
          <el-input v-model="form.pointno"></el-input>
        </el-form-item>
        <el-form-item label="巡检点名称" prop="pointname">
          <el-input v-model="form.pointname"></el-input>
        </el-form-item>
        <el-form-item label="巡检点地址" prop="address">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="title == '添加' ? add() : update()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style></style>